﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using V5_Model;

namespace V5_DAL
{
    public class DALConfig
    {
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "K_Config");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_Config");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelConfig model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_Config(");
            strSql.Append("ID,SiteTitle,SiteUrl,SiteLogo,WebmasterName,WebmasterEmail,SiteCopyright,SiteDescription,SiteKeywords,SiteState,SiteCloseText,MailObject,MailServer,MailServerUserName,MailServerPassWord,MailDomain,TemplatePath,UrlRewrite,UploadType,UploadMwidth,UploadSwidth)");
            strSql.Append(" values (");
            strSql.Append("@ID,@SiteTitle,@SiteUrl,@SiteLogo,@WebmasterName,@WebmasterEmail,@SiteCopyright,@SiteDescription,@SiteKeywords,@SiteState,@SiteCloseText,@MailObject,@MailServer,@MailServerUserName,@MailServerPassWord,@MailDomain,@TemplatePath,@UrlRewrite,@UploadType,@UploadMwidth,@UploadSwidth)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@SiteTitle", SqlDbType.NVarChar,255),
					new SqlParameter("@SiteUrl", SqlDbType.NVarChar,255),
					new SqlParameter("@SiteLogo", SqlDbType.NVarChar,255),
					new SqlParameter("@WebmasterName", SqlDbType.NVarChar,50),
					new SqlParameter("@WebmasterEmail", SqlDbType.NVarChar,255),
					new SqlParameter("@SiteCopyright", SqlDbType.NVarChar),
					new SqlParameter("@SiteDescription", SqlDbType.NVarChar),
					new SqlParameter("@SiteKeywords", SqlDbType.NVarChar),
					new SqlParameter("@SiteState", SqlDbType.Int,4),
					new SqlParameter("@SiteCloseText", SqlDbType.NVarChar),
					new SqlParameter("@MailObject", SqlDbType.Int,4),
					new SqlParameter("@MailServer", SqlDbType.NVarChar,255),
					new SqlParameter("@MailServerUserName", SqlDbType.NVarChar,255),
					new SqlParameter("@MailServerPassWord", SqlDbType.NVarChar,255),
					new SqlParameter("@MailDomain", SqlDbType.NVarChar,255),
					new SqlParameter("@TemplatePath", SqlDbType.NVarChar,50),
					new SqlParameter("@UrlRewrite", SqlDbType.NVarChar,50),
					new SqlParameter("@UploadType", SqlDbType.NVarChar,255),
					new SqlParameter("@UploadMwidth", SqlDbType.NVarChar,50),
					new SqlParameter("@UploadSwidth", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.SiteTitle;
            parameters[2].Value = model.SiteUrl;
            parameters[3].Value = model.SiteLogo;
            parameters[4].Value = model.WebmasterName;
            parameters[5].Value = model.WebmasterEmail;
            parameters[6].Value = model.SiteCopyright;
            parameters[7].Value = model.SiteDescription;
            parameters[8].Value = model.SiteKeywords;
            parameters[9].Value = model.SiteState;
            parameters[10].Value = model.SiteCloseText;
            parameters[11].Value = model.MailObject;
            parameters[12].Value = model.MailServer;
            parameters[13].Value = model.MailServerUserName;
            parameters[14].Value = model.MailServerPassWord;
            parameters[15].Value = model.MailDomain;
            parameters[16].Value = model.TemplatePath;
            parameters[17].Value = model.UrlRewrite;
            parameters[18].Value = model.UploadType;
            parameters[19].Value = model.UploadMwidth;
            parameters[20].Value = model.UploadSwidth;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelConfig model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_Config set ");
            strSql.Append("SiteTitle=@SiteTitle,");
            strSql.Append("SiteUrl=@SiteUrl,");
            strSql.Append("SiteLogo=@SiteLogo,");
            strSql.Append("WebmasterName=@WebmasterName,");
            strSql.Append("WebmasterEmail=@WebmasterEmail,");
            strSql.Append("SiteCopyright=@SiteCopyright,");
            strSql.Append("SiteDescription=@SiteDescription,");
            strSql.Append("SiteKeywords=@SiteKeywords,");
            strSql.Append("SiteState=@SiteState,");
            strSql.Append("SiteCloseText=@SiteCloseText,");
            strSql.Append("MailObject=@MailObject,");
            strSql.Append("MailServer=@MailServer,");
            strSql.Append("MailServerUserName=@MailServerUserName,");
            strSql.Append("MailServerPassWord=@MailServerPassWord,");
            strSql.Append("MailDomain=@MailDomain,");
            strSql.Append("TemplatePath=@TemplatePath,");
            strSql.Append("UrlRewrite=@UrlRewrite,");
            strSql.Append("UploadType=@UploadType,");
            strSql.Append("UploadMwidth=@UploadMwidth,");
            strSql.Append("UploadSwidth=@UploadSwidth");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@SiteTitle", SqlDbType.NVarChar,255),
					new SqlParameter("@SiteUrl", SqlDbType.NVarChar,255),
					new SqlParameter("@SiteLogo", SqlDbType.NVarChar,255),
					new SqlParameter("@WebmasterName", SqlDbType.NVarChar,50),
					new SqlParameter("@WebmasterEmail", SqlDbType.NVarChar,255),
					new SqlParameter("@SiteCopyright", SqlDbType.NVarChar),
					new SqlParameter("@SiteDescription", SqlDbType.NVarChar),
					new SqlParameter("@SiteKeywords", SqlDbType.NVarChar),
					new SqlParameter("@SiteState", SqlDbType.Int,4),
					new SqlParameter("@SiteCloseText", SqlDbType.NVarChar),
					new SqlParameter("@MailObject", SqlDbType.Int,4),
					new SqlParameter("@MailServer", SqlDbType.NVarChar,255),
					new SqlParameter("@MailServerUserName", SqlDbType.NVarChar,255),
					new SqlParameter("@MailServerPassWord", SqlDbType.NVarChar,255),
					new SqlParameter("@MailDomain", SqlDbType.NVarChar,255),
					new SqlParameter("@TemplatePath", SqlDbType.NVarChar,50),
					new SqlParameter("@UrlRewrite", SqlDbType.NVarChar,50),
					new SqlParameter("@UploadType", SqlDbType.NVarChar,255),
					new SqlParameter("@UploadMwidth", SqlDbType.NVarChar,50),
					new SqlParameter("@UploadSwidth", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.SiteTitle;
            parameters[2].Value = model.SiteUrl;
            parameters[3].Value = model.SiteLogo;
            parameters[4].Value = model.WebmasterName;
            parameters[5].Value = model.WebmasterEmail;
            parameters[6].Value = model.SiteCopyright;
            parameters[7].Value = model.SiteDescription;
            parameters[8].Value = model.SiteKeywords;
            parameters[9].Value = model.SiteState;
            parameters[10].Value = model.SiteCloseText;
            parameters[11].Value = model.MailObject;
            parameters[12].Value = model.MailServer;
            parameters[13].Value = model.MailServerUserName;
            parameters[14].Value = model.MailServerPassWord;
            parameters[15].Value = model.MailDomain;
            parameters[16].Value = model.TemplatePath;
            parameters[17].Value = model.UrlRewrite;
            parameters[18].Value = model.UploadType;
            parameters[19].Value = model.UploadMwidth;
            parameters[20].Value = model.UploadSwidth;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Config ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelConfig GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,SiteTitle,SiteUrl,SiteLogo,WebmasterName,WebmasterEmail,SiteCopyright,SiteDescription,SiteKeywords,SiteState,SiteCloseText,MailObject,MailServer,MailServerUserName,MailServerPassWord,MailDomain,TemplatePath,UrlRewrite,UploadType,UploadMwidth,UploadSwidth from K_Config ");
            //strSql.Append(" where ID=@ID ");
            //SqlParameter[] parameters = {
					//new SqlParameter("@ID", SqlDbType.Int,4)};
            //parameters[0].Value = ID;

            ModelConfig model = new ModelConfig();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.SiteTitle = ds.Tables[0].Rows[0]["SiteTitle"].ToString();
                model.SiteUrl = ds.Tables[0].Rows[0]["SiteUrl"].ToString();
                model.SiteLogo = ds.Tables[0].Rows[0]["SiteLogo"].ToString();
                model.WebmasterName = ds.Tables[0].Rows[0]["WebmasterName"].ToString();
                model.WebmasterEmail = ds.Tables[0].Rows[0]["WebmasterEmail"].ToString();
                model.SiteCopyright = ds.Tables[0].Rows[0]["SiteCopyright"].ToString();
                model.SiteDescription = ds.Tables[0].Rows[0]["SiteDescription"].ToString();
                model.SiteKeywords = ds.Tables[0].Rows[0]["SiteKeywords"].ToString();
                if (ds.Tables[0].Rows[0]["SiteState"].ToString() != "")
                {
                    model.SiteState = int.Parse(ds.Tables[0].Rows[0]["SiteState"].ToString());
                }
                model.SiteCloseText = ds.Tables[0].Rows[0]["SiteCloseText"].ToString();
                if (ds.Tables[0].Rows[0]["MailObject"].ToString() != "")
                {
                    model.MailObject = int.Parse(ds.Tables[0].Rows[0]["MailObject"].ToString());
                }
                model.MailServer = ds.Tables[0].Rows[0]["MailServer"].ToString();
                model.MailServerUserName = ds.Tables[0].Rows[0]["MailServerUserName"].ToString();
                model.MailServerPassWord = ds.Tables[0].Rows[0]["MailServerPassWord"].ToString();
                model.MailDomain = ds.Tables[0].Rows[0]["MailDomain"].ToString();
                model.TemplatePath = ds.Tables[0].Rows[0]["TemplatePath"].ToString();
                model.UrlRewrite = ds.Tables[0].Rows[0]["UrlRewrite"].ToString();
                model.UploadType = ds.Tables[0].Rows[0]["UploadType"].ToString();
                model.UploadMwidth = ds.Tables[0].Rows[0]["UploadMwidth"].ToString();
                model.UploadSwidth = ds.Tables[0].Rows[0]["UploadSwidth"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,SiteTitle,SiteUrl,SiteLogo,WebmasterName,WebmasterEmail,SiteCopyright,SiteDescription,SiteKeywords,SiteState,SiteCloseText,MailObject,MailServer,MailServerUserName,MailServerPassWord,MailDomain,TemplatePath,UrlRewrite,UploadType,UploadMwidth,UploadSwidth ");
            strSql.Append(" FROM K_Config ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,SiteTitle,SiteUrl,SiteLogo,WebmasterName,WebmasterEmail,SiteCopyright,SiteDescription,SiteKeywords,SiteState,SiteCloseText,MailObject,MailServer,MailServerUserName,MailServerPassWord,MailDomain,TemplatePath,UrlRewrite,UploadType,UploadMwidth,UploadSwidth ");
            strSql.Append(" FROM K_Config ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_Config";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  成员方法
    }
}
